#include "../head/taskQueue.h"

// 初始化任务队列
int initQueue(taskQueue_t *pqueue){
    bzero(pqueue, sizeof(taskQueue_t));
    pthread_mutex_init(&pqueue->mutex, NULL);
    pthread_cond_init(&pqueue->cond, NULL);
    return 0;
}

// 任务入队
int enQueue(taskQueue_t *pqueue, int net_fd){
    task_t * pNew = (task_t *)malloc(sizeof(task_t));
    pNew->net_fd = net_fd;
    pNew->next = NULL;

    if(pqueue->size == 0) {
        pqueue->front = pNew;
        pqueue->rear = pNew;
    }
    else {
        pqueue->rear->next = pNew;
        pqueue->rear = pNew;
    }
    ++pqueue->size;

    return 0;
}

// 任务出队
int deQueue(taskQueue_t *pqueue){
    task_t * pCur = pqueue->front;
    pqueue->front = pCur->next;
    free(pCur);
    if(pqueue->size == 1){
        pqueue->rear = NULL;
    }
    --pqueue->size;
    return 0;
}

